<template>
    <el-dialog :title="title" v-model="open" width="680px" append-to-body @close="close">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="协议名称" prop="modelName">
                <el-input v-model="form.modelName" placeholder="协议名称"></el-input>
            </el-form-item>
            <el-form-item label="描述" prop="description">
                <el-input v-model="form.description" placeholder="协议名称"></el-input>
            </el-form-item>
        </el-form>
        <div align="center" slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import {addAgreementModel, updateAgreementModel} from "/src/api/property/model";

    export default {
        name: "update",
        emits: ['dialogStatusChanged'],
        props: {
            title: String,
            open: Boolean,
            form: Object,
        },
        data() {
            return {
                // 表单校验
                rules: {
                    modelName: [
                        {required: true, message: "协议名称不能为空", trigger: "blur"}
                    ],
                }
            }
        },
        methods: {
            //提交
            submitForm(){
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        if (this.form.modelId !== undefined) {
                            updateAgreementModel(this.form).then(response => {
                                this.msgSuccess("修改成功");
                                this.close();
                            });
                        } else {
                            addAgreementModel(this.form).then(response => {
                                this.msgSuccess("新增成功");
                                this.close();
                            });
                        }
                    }
                })
            },
            //取消
            cancel() {
                this.$emit('dialogStatusChanged', false)
            },
            //关闭
            close() {
                this.$emit('dialogStatusChanged', false)
            }
        }
    }
</script>

<style scoped>

</style>
